package com.bjpowernode.service.impl;

import com.bjpowernode.mapper.TActivityMapper;
import com.bjpowernode.mapper.TClueMapper;
import com.bjpowernode.mapper.TCustomerMapper;
import com.bjpowernode.mapper.TTranMapper;
import com.bjpowernode.result.NameValueData;
import com.bjpowernode.result.SummaryData;
import com.bjpowernode.service.StatisticsService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class StatisticsServiceImpl implements StatisticsService {

    @Resource
    private TActivityMapper tActivityMapper;

    @Resource
    private TClueMapper tClueMapper;

    @Resource
    private TTranMapper tTranMapper;

    @Resource
    private TCustomerMapper tCustomerMapper;

    @Override
    public SummaryData getSummaryData() {

        //进行中的市场活动
        int goingActivityCount = tActivityMapper.selectGoingActivityCount();
        //所有的市场活动
        int totalActivityCount = tActivityMapper.selectTotalActivityCount();

        //所有的线索
        int totalClueCount = tClueMapper.selectTotalClueCount();

        //所有的客户
        int totalCustomerCount = tCustomerMapper.selectTotalCustomerCount();

        //成功交易额
        BigDecimal successTranAmount = tTranMapper.selectSuccessTranAmount();
        //总交易额
        BigDecimal totalTranAmount = tTranMapper.selectTotalTranAmount();


        SummaryData summaryData = SummaryData.builder()
                .goingActivityCount(goingActivityCount)
                .totalActivityCount(totalActivityCount)
                .totalClueCount(totalClueCount)
                .totalCustomerCount(totalCustomerCount)
                .successTranAmount(successTranAmount)
                .totalTranAmount(totalTranAmount).build();

        return summaryData;
    }


    /**
     * 查询销售漏斗
     * @return
     */
    @Override
    public List<NameValueData> getSaleFunnelData() {
        List<NameValueData> nameValueDataList = new ArrayList<>();

        //总线索
        int totalClueCount = tClueMapper.selectTotalClueCount();
        NameValueData clueData = NameValueData.builder().name("线索").value(totalClueCount).build();
        nameValueDataList.add(clueData);

        //总客户
        int totalCustomerCount = tCustomerMapper.selectTotalCustomerCount();
        NameValueData customerData = NameValueData.builder().name("客户").value(totalCustomerCount).build();
        nameValueDataList.add(customerData);

        //总交易
        int totalTranCount = tTranMapper.selectTotalTranCount();
        NameValueData tranData = NameValueData.builder().name("交易").value(totalTranCount).build();
        nameValueDataList.add(tranData);

        //成交
        int totalSuccessTranCount = tTranMapper.selectSuccessTranCount();
        NameValueData successTranData = NameValueData.builder().name("成交").value(totalSuccessTranCount).build();
        nameValueDataList.add(successTranData);

        return nameValueDataList;
    }

    @Override
    public List<NameValueData> getCluePieData() {
        return tClueMapper.selectClueSourceCount();
    }
}
